// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.iso.areas;

import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Stack;
import java.util.Vector;
import zombie.characters.IsoGameCharacter;
import zombie.characters.IsoPlayer;
import zombie.characters.SurvivorDesc;
import zombie.core.Rand;
import zombie.core.opengl.RenderSettings;
import zombie.inventory.ItemContainer;
import zombie.inventory.ItemType;
import zombie.iso.BuildingDef;
import zombie.iso.IsoCell;
import zombie.iso.IsoGridSquare;
import zombie.iso.IsoLightSource;
import zombie.iso.IsoObject;
import zombie.iso.IsoUtils;
import zombie.iso.IsoWorld;
import zombie.iso.LotHeader;
import zombie.iso.RoomDef;
import zombie.iso.SpriteDetails.IsoFlagType;
import zombie.iso.objects.IsoDoor;
import zombie.iso.objects.IsoWindow;

public final class IsoBuilding extends IsoArea {
    public Rectangle bounds;
    public final Vector<IsoRoomExit> Exits = new Vector();
    public boolean IsResidence = true;
    public final ArrayList<ItemContainer> container = new ArrayList();
    public final Vector<IsoRoom> Rooms = new Vector();
    public final Vector<IsoWindow> Windows = new Vector();
    public int ID = 0;
    public static int IDMax = 0;
    public int safety = 0;
    public int transparentWalls = 0;
    private boolean isToxic = false;
    public static float PoorBuildingScore = 10.0F;
    public static float GoodBuildingScore = 100.0F;
    public int scoreUpdate = -1;
    public BuildingDef def;
    public boolean bSeenInside = false;
    public ArrayList<IsoLightSource> lights = new ArrayList();
    static ArrayList<IsoRoom> tempo = new ArrayList();
    static ArrayList<ItemContainer> tempContainer = new ArrayList();
    static ArrayList<String> RandomContainerChoices = new ArrayList();
    static ArrayList<IsoWindow> windowchoices = new ArrayList();

    public int getRoomsNumber() {
        return this.Rooms.size();
    }

    public IsoBuilding() {
        this.ID = IDMax++;
        this.scoreUpdate = -120 + Rand.Next(120);
    }

    public int getID() {
        return this.ID;
    }

    public void TriggerAlarm() {
    }

    public IsoBuilding(IsoCell cell) {
        this.ID = IDMax++;
        this.scoreUpdate = -120 + Rand.Next(120);
    }

    public boolean ContainsAllItems(Stack<String> items) {
        return false;
    }

    public float ScoreBuildingPersonSpecific(SurvivorDesc desc, boolean bFarGood) {
        float float0 = 0.0F;
        float0 += (float)(this.Rooms.size() * 5);
        float0 += (float)(this.Exits.size() * 15);
        float0 -= (float)(this.transparentWalls * 10);

        for (int int0 = 0; int0 < this.container.size(); int0++) {
            ItemContainer itemContainer = (ItemContainer)this.container.get(int0);
            float0 += (float)(itemContainer.Items.size() * 3);
        }

        if (!IsoWorld.instance.CurrentCell.getBuildingScores().containsKey(this.ID)) {
            BuildingScore buildingScore0 = new BuildingScore(this);
            buildingScore0.building = this;
            IsoWorld.instance.CurrentCell.getBuildingScores().put(this.ID, buildingScore0);
            this.ScoreBuildingGeneral(buildingScore0);
        }

        BuildingScore buildingScore1 = (BuildingScore)IsoWorld.instance.CurrentCell.getBuildingScores().get(this.ID);
        float0 += (buildingScore1.defense + buildingScore1.food + (float)buildingScore1.size + buildingScore1.weapons + buildingScore1.wood) * 10.0F;
        int int1 = -10000;
        int int2 = -10000;
        if (!this.Exits.isEmpty()) {
            IsoRoomExit isoRoomExit = (IsoRoomExit)this.Exits.get(0);
            int1 = isoRoomExit.x;
            int2 = isoRoomExit.y;
        }

        float float1 = IsoUtils.DistanceManhatten(desc.getInstance().getX(), desc.getInstance().getY(), (float)int1, (float)int2);
        if (float1 > 0.0F) {
            if (bFarGood) {
                float0 *= float1 * 0.5F;
            } else {
                float0 /= float1 * 0.5F;
            }
        }

        return float0;
    }

    public BuildingDef getDef() {
        return this.def;
    }

    public void update() {
        if (!this.Exits.isEmpty()) {
            int int0 = 0;
            int int1 = 0;

            for (int int2 = 0; int2 < this.Rooms.size(); int2++) {
                IsoRoom isoRoom = (IsoRoom)this.Rooms.get(int2);
                if (isoRoom.layer == 0) {
                    for (int int3 = 0; int3 < isoRoom.TileList.size(); int3++) {
                        int1++;
                        IsoGridSquare isoGridSquare = (IsoGridSquare)isoRoom.TileList.get(int3);
                    }
                }
            }

            if (int1 == 0) {
                int1++;
            }

            int0 = (int)((float)int0 / (float)int1);
            this.scoreUpdate--;
            if (this.scoreUpdate <= 0) {
                this.scoreUpdate += 120;
                BuildingScore buildingScore = null;
                if (IsoWorld.instance.CurrentCell.getBuildingScores().containsKey(this.ID)) {
                    buildingScore = (BuildingScore)IsoWorld.instance.CurrentCell.getBuildingScores().get(this.ID);
                } else {
                    buildingScore = new BuildingScore(this);
                    buildingScore.building = this;
                }

                buildingScore = this.ScoreBuildingGeneral(buildingScore);
                buildingScore.defense += (float)(int0 * 10);
                this.safety = int0;
                IsoWorld.instance.CurrentCell.getBuildingScores().put(this.ID, buildingScore);
            }
        }
    }

    public void AddRoom(IsoRoom room) {
        this.Rooms.add(room);
        if (this.bounds == null) {
            this.bounds = (Rectangle)room.bounds.clone();
        }

        if (room != null && room.bounds != null) {
            this.bounds.add(room.bounds);
        }
    }

    public void CalculateExits() {
        for (IsoRoom isoRoom : this.Rooms) {
            for (IsoRoomExit isoRoomExit : isoRoom.Exits) {
                if (isoRoomExit.To.From == null && isoRoom.layer == 0) {
                    this.Exits.add(isoRoomExit);
                }
            }
        }
    }

    public void CalculateWindows() {
        for (IsoRoom isoRoom : this.Rooms) {
            for (IsoGridSquare isoGridSquare0 : isoRoom.TileList) {
                IsoGridSquare isoGridSquare1 = isoGridSquare0.getCell().getGridSquare(isoGridSquare0.getX(), isoGridSquare0.getY() + 1, isoGridSquare0.getZ());
                IsoGridSquare isoGridSquare2 = isoGridSquare0.getCell().getGridSquare(isoGridSquare0.getX() + 1, isoGridSquare0.getY(), isoGridSquare0.getZ());
                if (isoGridSquare0.getProperties().Is(IsoFlagType.collideN) && isoGridSquare0.getProperties().Is(IsoFlagType.transparentN)) {
                    isoRoom.transparentWalls++;
                    this.transparentWalls++;
                }

                if (isoGridSquare0.getProperties().Is(IsoFlagType.collideW) && isoGridSquare0.getProperties().Is(IsoFlagType.transparentW)) {
                    isoRoom.transparentWalls++;
                    this.transparentWalls++;
                }

                if (isoGridSquare1 != null) {
                    boolean boolean0 = isoGridSquare1.getRoom() != null;
                    if (isoGridSquare1.getRoom() != null && isoGridSquare1.getRoom().building != isoRoom.building) {
                        boolean0 = false;
                    }

                    if (isoGridSquare1.getProperties().Is(IsoFlagType.collideN) && isoGridSquare1.getProperties().Is(IsoFlagType.transparentN) && !boolean0) {
                        isoRoom.transparentWalls++;
                        this.transparentWalls++;
                    }
                }

                if (isoGridSquare2 != null) {
                    boolean boolean1 = isoGridSquare2.getRoom() != null;
                    if (isoGridSquare2.getRoom() != null && isoGridSquare2.getRoom().building != isoRoom.building) {
                        boolean1 = false;
                    }

                    if (isoGridSquare2.getProperties().Is(IsoFlagType.collideW) && isoGridSquare2.getProperties().Is(IsoFlagType.transparentW) && !boolean1) {
                        isoRoom.transparentWalls++;
                        this.transparentWalls++;
                    }
                }

                for (int int0 = 0; int0 < isoGridSquare0.getSpecialObjects().size(); int0++) {
                    IsoObject isoObject0 = (IsoObject)isoGridSquare0.getSpecialObjects().get(int0);
                    if (isoObject0 instanceof IsoWindow) {
                        this.Windows.add((IsoWindow)isoObject0);
                    }
                }

                if (isoGridSquare1 != null) {
                    for (int int1 = 0; int1 < isoGridSquare1.getSpecialObjects().size(); int1++) {
                        IsoObject isoObject1 = (IsoObject)isoGridSquare1.getSpecialObjects().get(int1);
                        if (isoObject1 instanceof IsoWindow) {
                            this.Windows.add((IsoWindow)isoObject1);
                        }
                    }
                }

                if (isoGridSquare2 != null) {
                    for (int int2 = 0; int2 < isoGridSquare2.getSpecialObjects().size(); int2++) {
                        IsoObject isoObject2 = (IsoObject)isoGridSquare2.getSpecialObjects().get(int2);
                        if (isoObject2 instanceof IsoWindow) {
                            this.Windows.add((IsoWindow)isoObject2);
                        }
                    }
                }
            }
        }
    }

    public void FillContainers() {
        boolean _boolean = false;

        for (IsoRoom isoRoom : this.Rooms) {
            if (isoRoom.RoomDef != null && isoRoom.RoomDef.contains("tutorial")) {
                _boolean = true;
            }

            if (!isoRoom.TileList.isEmpty()) {
                IsoGridSquare isoGridSquare0 = (IsoGridSquare)isoRoom.TileList.get(0);
                if (isoGridSquare0.getX() < 74 && isoGridSquare0.getY() < 32) {
                    _boolean = true;
                }
            }

            if (isoRoom.RoomDef.contains("shop")) {
                this.IsResidence = false;
            }

            for (IsoGridSquare isoGridSquare1 : isoRoom.TileList) {
                for (int _int = 0; _int < isoGridSquare1.getObjects().size(); _int++) {
                    IsoObject isoObject = isoGridSquare1.getObjects().get(_int);
                    if (isoObject.hasWater()) {
                        isoRoom.getWaterSources().add(isoObject);
                    }

                    if (isoObject.container != null) {
                        this.container.add(isoObject.container);
                        isoRoom.Containers.add(isoObject.container);
                    }
                }

                if (isoGridSquare1.getProperties().Is(IsoFlagType.bed)) {
                    isoRoom.Beds.add(isoGridSquare1);
                }
            }
        }
    }

    public ItemContainer getContainerWith(ItemType itemType) {
        for (IsoRoom isoRoom : this.Rooms) {
            for (ItemContainer itemContainer : isoRoom.Containers) {
                if (itemContainer.HasType(itemType)) {
                    return itemContainer;
                }
            }
        }

        return null;
    }

    public IsoRoom getRandomRoom() {
        return this.Rooms.size() == 0 ? null : (IsoRoom)this.Rooms.get(Rand.Next(this.Rooms.size()));
    }

    private BuildingScore ScoreBuildingGeneral(BuildingScore buildingScore) {
        buildingScore.food = 0.0F;
        buildingScore.defense = 0.0F;
        buildingScore.weapons = 0.0F;
        buildingScore.wood = 0.0F;
        buildingScore.building = this;
        buildingScore.size = 0;
        buildingScore.defense = buildingScore.defense + (float)((this.Exits.size() - 1) * 140);
        buildingScore.defense = buildingScore.defense - (float)(this.transparentWalls * 40);
        buildingScore.size = this.Rooms.size() * 10;
        buildingScore.size = buildingScore.size + this.container.size() * 10;
        return buildingScore;
    }

    public IsoGridSquare getFreeTile() {
        IsoGridSquare isoGridSquare = null;

        do {
            IsoRoom isoRoom = (IsoRoom)this.Rooms.get(Rand.Next(this.Rooms.size()));
            isoGridSquare = isoRoom.getFreeTile();
        } while (isoGridSquare == null);

        return isoGridSquare;
    }

    public boolean hasWater() {
        Iterator iterator = this.Rooms.iterator();

        while (iterator != null && iterator.hasNext()) {
            IsoRoom isoRoom = (IsoRoom)iterator.next();
            if (!isoRoom.WaterSources.isEmpty()) {
                IsoObject isoObject = null;
                int _int = 0;

                while (true) {
                    if (_int < isoRoom.WaterSources.size()) {
                        if (!((IsoObject)isoRoom.WaterSources.get(_int)).hasWater()) {
                            _int++;
                            continue;
                        }

                        isoObject = (IsoObject)isoRoom.WaterSources.get(_int);
                    }

                    if (isoObject != null) {
                        return true;
                    }
                    break;
                }
            }
        }

        return false;
    }

    public void CreateFrom(BuildingDef building, LotHeader info) {
        for (int _int = 0; _int < building.rooms.size(); _int++) {
            IsoRoom isoRoom = info.getRoom(((RoomDef)building.rooms.get(_int)).ID);
            isoRoom.building = this;
            this.Rooms.add(isoRoom);
        }
    }

    public void setAllExplored(boolean b) {
        this.def.bAlarmed = false;

        for (int int0 = 0; int0 < this.Rooms.size(); int0++) {
            IsoRoom isoRoom = (IsoRoom)this.Rooms.get(int0);
            isoRoom.def.setExplored(b);

            for (int int1 = isoRoom.def.getX(); int1 <= isoRoom.def.getX2(); int1++) {
                for (int int2 = isoRoom.def.getY(); int2 <= isoRoom.def.getY2(); int2++) {
                    IsoGridSquare isoGridSquare = IsoWorld.instance.CurrentCell.getGridSquare(int1, int2, isoRoom.def.level);
                    if (isoGridSquare != null) {
                        isoGridSquare.setHourSeenToCurrent();
                    }
                }
            }
        }
    }

    public boolean isAllExplored() {
        for (int _int = 0; _int < this.Rooms.size(); _int++) {
            IsoRoom isoRoom = (IsoRoom)this.Rooms.get(_int);
            if (!isoRoom.def.bExplored) {
                return false;
            }
        }

        return true;
    }

    public void addWindow(IsoWindow obj, boolean bOtherTile, IsoGridSquare from, IsoBuilding building) {
        this.Windows.add(obj);
        IsoGridSquare isoGridSquare = null;
        if (bOtherTile) {
            isoGridSquare = obj.square;
        } else {
            isoGridSquare = from;
        }

        if (isoGridSquare != null) {
            if (isoGridSquare.getRoom() == null) {
                float float0 = RenderSettings.getInstance().getAmbientForPlayer(IsoPlayer.getPlayerIndex()) * 0.7F;
                float float1 = RenderSettings.getInstance().getAmbientForPlayer(IsoPlayer.getPlayerIndex()) * 0.7F;
                float float2 = RenderSettings.getInstance().getAmbientForPlayer(IsoPlayer.getPlayerIndex()) * 0.7F;
                byte _byte = 7;
                IsoLightSource isoLightSource = new IsoLightSource(
                    isoGridSquare.getX(), isoGridSquare.getY(), isoGridSquare.getZ(), float0, float1, float2, _byte, building
                );
                this.lights.add(isoLightSource);
                IsoWorld.instance.CurrentCell.getLamppostPositions().add(isoLightSource);
            }
        }
    }

    public void addWindow(IsoWindow obj, boolean bOtherTile) {
        this.addWindow(obj, bOtherTile, obj.square, null);
    }

    public void addDoor(IsoDoor obj, boolean bOtherTile, IsoGridSquare from, IsoBuilding building) {
        IsoGridSquare isoGridSquare = null;
        if (bOtherTile) {
            isoGridSquare = obj.square;
        } else {
            isoGridSquare = from;
        }

        if (isoGridSquare != null) {
            if (isoGridSquare.getRoom() == null) {
                float float0 = RenderSettings.getInstance().getAmbientForPlayer(IsoPlayer.getPlayerIndex()) * 0.7F;
                float float1 = RenderSettings.getInstance().getAmbientForPlayer(IsoPlayer.getPlayerIndex()) * 0.7F;
                float float2 = RenderSettings.getInstance().getAmbientForPlayer(IsoPlayer.getPlayerIndex()) * 0.7F;
                byte _byte = 7;
                IsoLightSource isoLightSource = new IsoLightSource(
                    isoGridSquare.getX(), isoGridSquare.getY(), isoGridSquare.getZ(), float0, float1, float2, _byte, building
                );
                this.lights.add(isoLightSource);
                IsoWorld.instance.CurrentCell.getLamppostPositions().add(isoLightSource);
            }
        }
    }

    public void addDoor(IsoDoor obj, boolean bOtherTile) {
        this.addDoor(obj, bOtherTile, obj.square, null);
    }

    public boolean isResidential() {
        return this.containsRoom("bedroom");
    }

    public boolean containsRoom(String room) {
        for (int _int = 0; _int < this.Rooms.size(); _int++) {
            if (room.equals(((IsoRoom)this.Rooms.get(_int)).getName())) {
                return true;
            }
        }

        return false;
    }

    public IsoRoom getRandomRoom(String room) {
        tempo.clear();

        for (int _int = 0; _int < this.Rooms.size(); _int++) {
            if (room.equals(((IsoRoom)this.Rooms.get(_int)).getName())) {
                tempo.add((IsoRoom)this.Rooms.get(_int));
            }
        }

        return tempo.isEmpty() ? null : (IsoRoom)tempo.get(Rand.Next(tempo.size()));
    }

    public ItemContainer getRandomContainer(String type) {
        RandomContainerChoices.clear();
        String[] string = null;
        if (type != null) {
            string = type.split(",");
        }

        if (string != null) {
            for (int int0 = 0; int0 < string.length; int0++) {
                RandomContainerChoices.add(string[int0]);
            }
        }

        tempContainer.clear();

        for (int int1 = 0; int1 < this.Rooms.size(); int1++) {
            IsoRoom isoRoom = (IsoRoom)this.Rooms.get(int1);

            for (int int2 = 0; int2 < isoRoom.Containers.size(); int2++) {
                ItemContainer itemContainer = (ItemContainer)isoRoom.Containers.get(int2);
                if (type == null || RandomContainerChoices.contains(itemContainer.getType())) {
                    tempContainer.add(itemContainer);
                }
            }
        }

        return tempContainer.isEmpty() ? null : (ItemContainer)tempContainer.get(Rand.Next(tempContainer.size()));
    }

    public IsoWindow getRandomFirstFloorWindow() {
        windowchoices.clear();
        windowchoices.addAll(this.Windows);

        for (int _int = 0; _int < windowchoices.size(); _int++) {
            if (((IsoWindow)windowchoices.get(_int)).getZ() > 0.0F) {
                windowchoices.remove(_int);
            }
        }

        return !windowchoices.isEmpty() ? (IsoWindow)windowchoices.get(Rand.Next(windowchoices.size())) : null;
    }

    public boolean isToxic() {
        return this.isToxic;
    }

    public void setToxic(boolean _isToxic) {
        this.isToxic = _isToxic;
    }

    public void forceAwake() {
        for (int int0 = this.def.getX(); int0 <= this.def.getX2(); int0++) {
            for (int int1 = this.def.getY(); int1 <= this.def.getY2(); int1++) {
                for (int int2 = 0; int2 <= 4; int2++) {
                    IsoGridSquare isoGridSquare = IsoWorld.instance.CurrentCell.getGridSquare(int0, int1, int2);
                    if (isoGridSquare != null) {
                        for (int int3 = 0; int3 < isoGridSquare.getMovingObjects().size(); int3++) {
                            if (isoGridSquare.getMovingObjects().get(int3) instanceof IsoGameCharacter) {
                                ((IsoGameCharacter)isoGridSquare.getMovingObjects().get(int3)).forceAwake();
                            }
                        }
                    }
                }
            }
        }
    }
}
